package au.com.twosquared.animalfarm.entities;

import java.util.ArrayList;

import au.com.twosquared.animalfarm.map.Map;
import au.com.twosquared.animalfarm.sprites.Animation;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * Defines the properties of an entity allowing it to be mapped and move
 * 
 * @author Jan Martin <jan.martin@twosquared.com.au>
 * @version 1.0 2011-15-11
 * @since 2011-15-11
 * 
 */
public abstract class Entity {

	/**
	 * The x position of the entity
	 */
	public float x;
	/**
	 * The y position of the entity
	 */
	public float y;
	/**
	 * The x velocity of the entity
	 */
	public float xs;
	/**
	 * The y velocity of the entity
	 */
	public float ys;
	/**
	 * Horizontal dimensions
	 */
	public float width;
	/**
	 * Vertical dimension
	 */
	public float height;
	/**
	 * Sprite animations (if any)
	 */
	protected ArrayList<Animation> animations = new ArrayList<Animation>();
	/**
	 * Which state is the sprite in, can control current animation
	 * 
	 * @Jan -1 means it hasnt been initialized yet
	 * @Jan Maybe -2 can mean it needs to be removed?
	 */
	public int state = -1;
	
	public Entity(ArrayList<Animation> animations) {
		this.animations = animations;
	}

	/**
	 * Updates the entity's properties given a change in time
	 * 
	 * @param delta
	 *            the change in time
	 */
	public void update(long delta) {
		x += xs;
		y += ys;
		// Do game logic
		logic(delta);
		// Only step current animation
		if (state != -1) {
			animations.get(state).update(delta);
		}
	}

	/**
	 * AI and other stuff. Called every frame.
	 */
	public void logic(long delta) {

	}

	/**
	 * Collision between the map. Called every frame.
	 * 
	 * @param map
	 *            the current map
	 */
	public void collision(Map map) {
	
	}

	/**
	 * Collision between other entities. Called every frame.
	 * 
	 * @param entity
	 *            the entity with which collision is being checked
	 */
	public void collision(Entity entity) {

	}

	/**
	 * Returns true if two entities are touching based on bounding boxes
	 * 
	 * @param entity
	 *            the entity with which collision is being checked
	 */
	protected boolean collides(Entity entity) {
		if(entity.x < x + width && entity.x > x) {
			if(entity.y < y + height && entity.y > y) {
				return true;
			}
		}
		return false;
	}

	/**
	 * This should be used instead of manually allocating state as this will
	 * reset the animation at the same time
	 * 
	 * @param state
	 *            the state you want to set entity to
	 */
	public void setState(int state) {
		this.state = state;
		if (state != -1) {
			animations.get(state).reset();
		}
	}

	public void draw(SpriteBatch batch) {
		if (state != -1) {
			animations.get(state).draw(batch, x, y);
		}
	}

}
